#include "umAvTask.h"
#include "umapi_libc.h"
#include "umlog.h"

extern S_SOURCE * createNewSource(UM_CHAR * inUrl);
extern S_SINK * createNewSink(UM_CHAR * outUrl);
#define NOUSE 0
UM_VOID *createAvTask(UM_CHAR *inUrl, UM_VOID **funcall, S_URLPARAM *pUrl)
{
	S_SOURCE *pSource = createNewSource(inUrl);
	if(!pSource)
		goto NOFOUND;
#if 0	
	if(inUrl)
	{
		if(pSource->open(pSource, inUrl)!=UM_SUCCESS)
		{
			goto OPENFAIL;
		}
	}
	else
	{
#endif	
		if(pSource->open2(pSource,  funcall)!=UM_SUCCESS)
		{
			goto OPENFAIL;
		}
#if 0		
	}
#endif	
	S_SINK *sink = UM_NULL;
	if(umapi_strlen(pUrl->hlsUrl) > 0)
	{
		sink = createNewSink(pUrl->hlsUrl);
		if(sink)
		{	
			LOGI("create new Hls Sink %s", pUrl->hlsUrl);
			sink->open(sink, pUrl->hlsUrl);
			pSource->addSink(pSource, sink);
		}
		else
			LOGE("create new Http Sink %s error", pUrl->hlsUrl);
	}
	if(umapi_strlen(pUrl->httpUrl) > 0)
	{
		sink = createNewSink(pUrl->httpUrl);
		if(sink)
		{	
			LOGI("create new Http Sink %s", pUrl->httpUrl);
			sink->open(sink, pUrl->httpUrl);
			pSource->addSink(pSource, sink);
		}
		else
			LOGE("create new Http Sink %s error", pUrl->httpUrl);
	}
	if(umapi_strlen(pUrl->rtspUrl) > 0)
	{
		sink = createNewSink(pUrl->rtspUrl);
		if(sink)
		{	
			LOGI("create new Rtsp Sink %s", pUrl->rtspUrl);
			sink->open(sink, pUrl->rtspUrl);
			pSource->addSink(pSource, sink);
		}
		else
			LOGE("create new Rtsp Sink %s error", pUrl->rtspUrl);
	}
	return pSource;
OPENFAIL:
	pSource->destroy(pSource);
	umapi_free(pSource);
NOFOUND:
	return UM_NULL;
}
UM_S32 stopAvTask(UM_VOID *pavTask)
{
	S_SOURCE *pSource = (S_SOURCE *)pavTask;
	pSource->stop(pSource);
	return UM_SUCCESS;
}
UM_S32 startAvTask(UM_VOID *pavTask)
{
	S_SOURCE *pSource = (S_SOURCE *)pavTask;
	pSource->start(pSource);
	return UM_SUCCESS;
}
UM_S32 destroyAvTask(UM_VOID *pavTask)
{
	S_SOURCE *pSource = (S_SOURCE *)pavTask;
	pSource->destroy(pSource);
	umapi_free(pavTask);
	return UM_SUCCESS;
}
UM_VOID AvTrace(UM_VOID *pavTask)
{
	S_SOURCE *pSource = (S_SOURCE *)pavTask;
	if(pSource)
	{
		if(pSource->trace)
			pSource->trace(pSource);
	}
}

